package com.jifujun.junbatis.base.util;


import com.jifujun.junbatis.base.service.ConditionConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * Create with IntelliJ IDEA
 * Project Name: junbatis
 * Package Name: club.emergency.junbatis.base.util
 * Date: 2018/3/30 14:12
 * User: 快乐的小菌菌
 * Description:
 */
public class SqlStringUtil {
    //下划线转驼峰
    public static String underlineHump(String arg) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < arg.length(); i++) {
            Character c = arg.charAt(i);
            if (i == 0) {
                stringBuilder.append(c.toString().toLowerCase());
                continue;
            }
            if (c.equals('_')) {
                i++;
                Character c1 = arg.charAt(i);
                stringBuilder.append(c1.toString().toUpperCase());
            } else {
                stringBuilder.append(c.toString());
            }
        }
        return stringBuilder.toString();
    }

    //驼峰转下划线
    public static String humpUnderline(String arg) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < arg.length(); i++) {
            Character c = arg.charAt(i);
            if (Character.isUpperCase(c)) {
                if (i != 0)
                    stringBuilder.append("_").append(c.toString().toLowerCase());
                else
                    stringBuilder.append(c.toString().toLowerCase());
            } else {
                stringBuilder.append(c);
            }
        }
        return stringBuilder.toString();
    }

    public static String firstCharLowerCase(String arg) {
        char[] chars = arg.toCharArray();
        if (Character.isUpperCase(chars[0])) {
            Character character = chars[0];
            chars[0] = character.toString().toLowerCase().charAt(0);
        }
        return new String(chars);
    }

    /**
     * 删除第一个和最后一个小括号，这个方法主要在{@link ConditionConstructor#insertMoreConstructor(List)}
     * 中把value以List的形式入参，返回类似 0, C1), (C2, C3), (C4, C5 这样的数据，我在封装insert批量的时候遇到了这种情况。因为mybatis api的原因，已经把开始的和最后的括号加上了，所以这里要处理下才能和mysql的语法对应上，这个方法主要做这件事情。
     *
     * @param stringList 传入的字符型List
     * @return list
     */
    public static List<String> parenthesisHandler(List<String> stringList, Class<?> clazz) {
        String[] columns = SqlUtil.sqlColumnBuilder(clazz, false, true);
        //这个对象一个有多少个属性，以便来划分insert中value组合
        List<String> resultList = new ArrayList<>();
        int flag = 0;
        for (int i = 0; i < stringList.size(); i++) {
            if (flag == 0 && i != 0) {
                resultList.add("(" + ArgIndex.ARG_C + i);
                flag++;
            } else if (flag == columns.length - 1 && i != stringList.size() - 1) {
                resultList.add(ArgIndex.ARG_C + i + ")");
                flag = 0;
            } else {
                resultList.add(ArgIndex.ARG_C + i);
                flag++;
            }
        }
        return resultList;
    }
}
